package com.ilearn.autocomplie.code;

import java.util.List;

/**
 * @Project: seb-core
 * @Description:
 * @Version 1.0
 * @Throws SystemException:
 * @Author: <li>2019/1/13 lyndon Create 1.0
 * @Copyright (c)2018-2018 中央结算公司，版权所有
 * @Modified By:
 */
public class GenerateCode {

    public String createCode(Class<?> type, String tableName, List<String> columnNames, List<String> columnTypes) {

        StringBuilder codeBuilder = new StringBuilder();

        codeBuilder.append("package ").append(type.getPackage().getName()).append(";");

        String entityName = firstUpperOtherLower(tableName);

        codeBuilder.append("\npublic class ").append(entityName).append("$Entity implements com.ilearn.autocomplie.entities.EntityModel ").append(" {");

        // generate properties
        for (int i = 0; i < columnNames.size(); i++) {
            codeBuilder.append("\nprivate ").append(columnTypes.get(i)).append(" ").append(columnNames.get(i)).append(" = null;");
        }

        // generate get methods
        for (int i = 0; i < columnNames.size(); i++) {
            codeBuilder.append("\npublic ").append(columnTypes.get(i)).append(" get").append(firstUpperOnly(columnNames.get(i))).append("() {");
            codeBuilder.append("\nreturn ").append(columnNames.get(i)).append(";");
            codeBuilder.append("\n}");
        }

        // generate set methods
        for (int i = 0; i < columnNames.size(); i++) {
            codeBuilder.append("\npublic void set").append(firstUpperOnly(columnNames.get(i))).append("(").append(columnTypes.get(i)).append(" ").append(firstLowerOnly(columnNames.get(i))).append(")").append("{");
            codeBuilder.append("\nthis.").append(columnNames.get(i)).append("=").append(firstLowerOnly(columnNames.get(i))).append(";");
            codeBuilder.append("\n}");
        }

        codeBuilder.append("\n}");




        return codeBuilder.toString();

    }

    private String firstUpperOtherLower(String orgiStr) {
        char[] chars = orgiStr.toCharArray();
        StringBuilder sb = new StringBuilder(128);
        for (int i = 0; i < chars.length; i++) {
            if (i != 0){
                sb.append(Character.toLowerCase(chars[i]));
            } else {
                sb.append(Character.toUpperCase(chars[i]));
            }
        }
        return sb.toString();
    }

    private String firstUpperOnly(String orgiStr) {
        char[] chars = orgiStr.toCharArray();
        StringBuilder sb = new StringBuilder(128);
        for (int i = 0; i < chars.length; i++) {
            if (i == 0){
                sb.append(Character.toUpperCase(chars[i]));
            } else {
                sb.append(chars[i]);
            }
        }
        return sb.toString();
    }

    private String firstLowerOnly(String orgiStr) {
        char[] chars = orgiStr.toCharArray();
        StringBuilder sb = new StringBuilder(128);
        for (int i = 0; i < chars.length; i++) {
            if (i == 0){
                sb.append(Character.toLowerCase(chars[i]));
            } else {
                sb.append(chars[i]);
            }
        }
        return sb.toString();
    }
}
